package pl.lodz.p.tewi;

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class OTPFrame extends javax.swing.JInternalFrame {

    private OTP otp=new OTP();
    private byte klucz[];
    private byte tekstJawny[];
    private byte szyfrogram[];
    private byte deszyfrogram[];
    private File plikOdczytuKlucza, plikZapisuKlucza, plikOdczytuTekstu, plikZapisuTekstu, plikOdczytuSzyfr, plikZapisuSzyfr;
 
    class OTPFilter extends javax.swing.filechooser.FileFilter {
        
        String ext;
        
        public OTPFilter()
        {
            ext=".otpKey";
        }
       
        @Override
        public boolean accept(File file)
        {  if (file.isDirectory()) return true;
    
            return file.getName().endsWith(ext);
        }
        
        @Override
        public String getDescription()
        {
            return "Plik klucza algorytmu OTP";
        }
    }
        
        class dl1 implements DocumentListener
        { 	public void insertUpdate(DocumentEvent e){jLabel7.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
                public void removeUpdate(DocumentEvent e){jLabel7.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
                public void changedUpdate(DocumentEvent e){jLabel7.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
        }
     class dl2 implements DocumentListener
        { 	public void insertUpdate(DocumentEvent e){jLabel8.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
                public void removeUpdate(DocumentEvent e){jLabel8.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
                public void changedUpdate(DocumentEvent e){jLabel8.setText("Ilość znaków: "+String.valueOf(e.getDocument().getLength())) ;}
        }
     class dl3 implements DocumentListener
        { 	public void insertUpdate(DocumentEvent e){
                double pom=e.getDocument().getLength();pom/=2;
                jLabel9.setText("Ilość znaków: "+String.valueOf(pom)) ;
                }
                public void removeUpdate(DocumentEvent e){
                double pom=e.getDocument().getLength();pom/=2;
                jLabel9.setText("Ilość znaków: "+String.valueOf(pom)) ;
                }
                public void changedUpdate(DocumentEvent e)
                {double pom=e.getDocument().getLength();pom/=2;
                jLabel9.setText("Ilość znaków: "+String.valueOf(pom)) ;
                }
         }
        
    
    
    public OTPFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
    {
       super(title, resizable, closable, maximizable, iconifiable);
        
        initComponents();
        jTextArea1.getDocument().addDocumentListener(new dl1());
        jTextArea2.getDocument().addDocumentListener(new dl2());
        jTextArea3.getDocument().addDocumentListener(new dl3());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        wczytajKlucz = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        zapiszKlucz = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        wczytajTekstJawny = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        zapiszTekstJawny = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        szyfrowanie = new javax.swing.JButton();
        deszyfrowanie = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        wczytajSzyfrogram = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea3 = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        zapiszSzyfrogram = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(800, 570));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Klucz"));

        jLabel1.setText("Podaj nazwę pliku");

        wczytajKlucz.setText("Otwórz");
        wczytajKlucz.setName(""); // NOI18N
        wczytajKlucz.setOpaque(false);
        wczytajKlucz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wczytajKluczActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel2.setText("Podaj nazwę pliku");

        zapiszKlucz.setText("Zapisz");
        zapiszKlucz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zapiszKluczActionPerformed(evt);
            }
        });

        jLabel7.setText("Ilość znaków:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(wczytajKlucz))
                    .addComponent(jLabel7)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(zapiszKlucz, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {wczytajKlucz, zapiszKlucz});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wczytajKlucz))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zapiszKlucz))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tekst jawny"));

        jLabel3.setText("Podaj nazwę pliku");

        wczytajTekstJawny.setText("Otwórz");
        wczytajTekstJawny.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wczytajTekstJawnyActionPerformed(evt);
            }
        });

        jTextArea2.setColumns(20);
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jLabel4.setText("Podaj nazwę pliku");

        zapiszTekstJawny.setText("Zapisz");
        zapiszTekstJawny.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zapiszTekstJawnyActionPerformed(evt);
            }
        });

        jLabel8.setText("Ilość znaków:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(wczytajTekstJawny))
                    .addComponent(jLabel8)
                    .addComponent(jLabel3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(zapiszTekstJawny, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wczytajTekstJawny))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zapiszTekstJawny))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Szyfrowanie"));

        szyfrowanie.setText("Szyfrowanie->");
        szyfrowanie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                szyfrowanieActionPerformed(evt);
            }
        });

        deszyfrowanie.setText("<-Deszyfrowanie");
        deszyfrowanie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deszyfrowanieActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Plik");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Okno");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(deszyfrowanie, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(szyfrowanie, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(szyfrowanie)
                .addGap(18, 18, 18)
                .addComponent(deszyfrowanie)
                .addGap(27, 27, 27)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Szyfrogram"));

        jLabel5.setText("Podaj nazwę pliku");

        wczytajSzyfrogram.setText("Otwórz");
        wczytajSzyfrogram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wczytajSzyfrogramActionPerformed(evt);
            }
        });

        jTextArea3.setColumns(20);
        jTextArea3.setLineWrap(true);
        jTextArea3.setRows(5);
        jScrollPane3.setViewportView(jTextArea3);

        jLabel6.setText("Podaj nazwę pliku");

        zapiszSzyfrogram.setText("Zapisz");
        zapiszSzyfrogram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zapiszSzyfrogramActionPerformed(evt);
            }
        });

        jLabel9.setText("Ilość znaków:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(wczytajSzyfrogram))
                    .addComponent(jLabel9)
                    .addComponent(jLabel5)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(zapiszSzyfrogram, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wczytajSzyfrogram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zapiszSzyfrogram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel1, jPanel2, jPanel4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void wczytajKluczActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wczytajKluczActionPerformed
        JFileChooser fch = new JFileChooser();
        int wybor =fch.showOpenDialog(this);
        if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikOdczytuKlucza=new File(pom);
            jTextField1.setText(pom);
           try
            {klucz=Auxx.wczytajZPliku(pom);
             jTextArea1.setText(new String(klucz));
            }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z otworzeniem pliku"+pom, "Problem z otworzeniem pliku", JOptionPane.ERROR_MESSAGE); };
        }

        
    }//GEN-LAST:event_wczytajKluczActionPerformed

    private void wczytajTekstJawnyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wczytajTekstJawnyActionPerformed
        JFileChooser fch = new JFileChooser();
        int wybor =fch.showOpenDialog(this);
        if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikOdczytuTekstu=new File(pom);
            jTextField3.setText(pom);
           try
            {tekstJawny=Auxx.wczytajZPliku(pom);
             jTextArea2.setText(new String(tekstJawny));
            }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z otworzeniem pliku"+pom, "Problem z otworzeniem pliku", JOptionPane.ERROR_MESSAGE); };
        }

    }//GEN-LAST:event_wczytajTekstJawnyActionPerformed

    private void wczytajSzyfrogramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wczytajSzyfrogramActionPerformed
        JFileChooser fch = new JFileChooser();
        int wybor =fch.showOpenDialog(this);
        if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikOdczytuSzyfr=new File(pom);
            jTextField5.setText(pom);
            try
            {szyfrogram=Auxx.wczytajZPliku(pom);
             jTextArea3.setText(new String(szyfrogram));
            }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z otworzeniem pliku"+pom, "Problem z otworzeniem pliku", JOptionPane.ERROR_MESSAGE); };

        }

    }//GEN-LAST:event_wczytajSzyfrogramActionPerformed

    private void zapiszKluczActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zapiszKluczActionPerformed
       JFileChooser fch = new JFileChooser();
       int wybor =fch.showSaveDialog(this);
       if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikZapisuKlucza=new File(pom);
            jTextField2.setText(pom);
            try{ 
                if(jRadioButton1.isSelected())//plik
                Auxx.zapiszDoPliku(klucz, pom);
                else  Auxx.zapiszDoPliku(jTextArea1.getText().getBytes(), pom);
               }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z zapisem do pliku"+pom, "Problem z zapisem do pliku", JOptionPane.ERROR_MESSAGE); };         

        }

    }//GEN-LAST:event_zapiszKluczActionPerformed

    private void zapiszSzyfrogramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zapiszSzyfrogramActionPerformed
       JFileChooser fch = new JFileChooser();
       int wybor =fch.showSaveDialog(this);
       if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikZapisuSzyfr=new File(pom);
            jTextField6.setText(pom);
            try{ 
                if(jRadioButton1.isSelected())//plik
                Auxx.zapiszDoPliku(szyfrogram, pom);
                else  Auxx.zapiszDoPliku(jTextArea3.getText().getBytes(), pom);
               }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z zapisem do pliku"+pom, "Problem z zapisem do pliku", JOptionPane.ERROR_MESSAGE); };         

        }

        
    }//GEN-LAST:event_zapiszSzyfrogramActionPerformed

    private void zapiszTekstJawnyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zapiszTekstJawnyActionPerformed
       JFileChooser fch = new JFileChooser();
       int wybor =fch.showSaveDialog(this);
       if (wybor == JFileChooser.APPROVE_OPTION) 
        {   String pom=fch.getSelectedFile().getPath();
            plikZapisuTekstu=new File(pom);
            jTextField4.setText(pom);
            try{ 
                if(jRadioButton1.isSelected())//plik
                Auxx.zapiszDoPliku(tekstJawny, pom);
                else  Auxx.zapiszDoPliku(jTextArea2.getText().getBytes(), pom);
               }catch(Exception e){JOptionPane.showMessageDialog(null, "Problem z zapisem do pliku"+pom, "Problem z zapisem do pliku", JOptionPane.ERROR_MESSAGE); };         

        }
    }//GEN-LAST:event_zapiszTekstJawnyActionPerformed

    private void szyfrowanieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_szyfrowanieActionPerformed
        try{            
             if(jRadioButton1.isSelected())//plik
               szyfrogram=otp.encode(tekstJawny,klucz);
            else szyfrogram=otp.encode(jTextArea2.getText().getBytes(),jTextArea1.getText().getBytes());
           jTextArea3.setText(Auxx.bytesToHex(szyfrogram));//new String(szyfrogram));
          } catch(OTP.OTPKeyException e){JOptionPane.showMessageDialog(null, e.getMessage(), "Problem z kluczem", JOptionPane.ERROR_MESSAGE); }

        
    }//GEN-LAST:event_szyfrowanieActionPerformed

    private void deszyfrowanieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deszyfrowanieActionPerformed
         try{            
             if(jRadioButton1.isSelected())//plik
               tekstJawny=otp.encode(szyfrogram,klucz);
            else tekstJawny=otp.decode(Auxx.hexToBytes(jTextArea3.getText()) /*jTextArea3.getText().getBytes()*/,jTextArea1.getText().getBytes());
           jTextArea2.setText(new String(tekstJawny));
          } catch(OTP.OTPKeyException e){JOptionPane.showMessageDialog(null, e.getMessage(), "Problem z kluczem", JOptionPane.ERROR_MESSAGE); }

    }//GEN-LAST:event_deszyfrowanieActionPerformed

   
      
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton deszyfrowanie;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextArea3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JButton szyfrowanie;
    private javax.swing.JButton wczytajKlucz;
    private javax.swing.JButton wczytajSzyfrogram;
    private javax.swing.JButton wczytajTekstJawny;
    private javax.swing.JButton zapiszKlucz;
    private javax.swing.JButton zapiszSzyfrogram;
    private javax.swing.JButton zapiszTekstJawny;
    // End of variables declaration//GEN-END:variables
}
